package com.kgcx.organization.service;

import com.github.pagehelper.PageHelper;
import com.kgcx.common.bean.RequestData;
import com.kgcx.common.constant.Cons;
import com.kgcx.dbutils.domain.BaseDomain;
import com.kgcx.dbutils.utils.PageUtils;
import com.kgcx.organization.domain.SuggestAddress;
import com.kgcx.organization.mapper.SuggestAddressMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
public class SuggestAddressService {
    @Autowired
    private SuggestAddressMapper suggestAddressMapper;

    public SuggestAddress getSuggestAddress(Long kid) {
        return suggestAddressMapper.selectByPrimaryKey(kid);
    }

    public List<SuggestAddress> getSuggestAddressList(RequestData rd) {
        PageUtils.page(rd);
        PageHelper.orderBy(rd.getOrderBy());
        return suggestAddressMapper.selectActive(BaseDomain.buildSelectDomain(rd, new SuggestAddress()));
    }

    @Transactional(rollbackFor = Exception.class)
    public void addSuggestAddress(SuggestAddress suggestAddress) {
        suggestAddressMapper.insertSelective(suggestAddress);
    }

    @Transactional(rollbackFor = Exception.class)
    public void updateSuggestAddress(SuggestAddress suggestAddress) {
        suggestAddressMapper.updateByPrimaryKeySelective(suggestAddress);
    }

    @Transactional(rollbackFor = Exception.class)
    public void removeSuggestAddress(Long kid) {
        SuggestAddress suggestAddress = new SuggestAddress();
        suggestAddress.setKid(kid);
        suggestAddress.setStatus(Cons.COMMON_STATUS_DELETE);
        suggestAddressMapper.updateByPrimaryKeySelective(suggestAddress);
    }
}
